package com.thti.mecool.client.util;

import java.io.DataOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

import com.thti.mecool.client.model.po.TaskManage;
import com.thti.mecool.client.service.Protocol;

public class FileUpload {

	@SuppressWarnings("unused")
	private static String actionUrl = Protocol.getURL
			+ Protocol.getUpload.replaceAll("_", "/");

	public static String upload_image(String actionUrl,List<TaskManage> list) {

		HttpURLConnection connection = null;
		DataOutputStream outputStream = null;
		StringBuffer sTaskId = new StringBuffer();
		StringBuffer sStepId = new StringBuffer();
		StringBuffer sProjectId = new StringBuffer();
		for (int j = 0; j < list.size(); j++) {
			if (j == (list.size() - 1)) {
				sTaskId.append(list.get(j).getTaskId());
				sProjectId.append(list.get(j).getProjectId());
				sStepId.append(list.get(j).getStepId());
			} else {
				sTaskId.append(list.get(j).getTaskId() + ",");
				sProjectId.append(list.get(j).getProjectId()+ ",");
				sStepId.append(list.get(j).getStepId() + ",");
			}
		}
		StringBuffer params = new StringBuffer();
		params.append("login_name=").append("jay");
		params.append("&plan_id=").append(list.get(0).getPlanId());
		params.append("&plan_job_id_list=").append(sTaskId);
		params.append("&plan_project_id_list=").append(sProjectId);
		params.append("&step_id_list=").append(sStepId);
		String urlServer = actionUrl + "?" + params;
		String lineEnd = "\r\n";
		String twoHyphens = "--";
		// String boundary = "*****";
		// 定义数据分隔线
		String BOUNDARY = "------------------------7dc2fd5c0894";
		try {
			URL url = new URL(urlServer);
			connection = (HttpURLConnection) url.openConnection();

			connection.setDoInput(true);
			connection.setDoOutput(true);
			connection.setUseCaches(false);

			connection.setRequestMethod("POST");
			connection.setRequestProperty("Connection", "Keep-Alive");
			connection.setRequestProperty("Content-Type",
					"multipart/form-data;boundary=" + BOUNDARY);
			outputStream = new DataOutputStream(connection.getOutputStream());
			for (int i = 0; i < list.size(); i++) {

				StringBuilder sb = new StringBuilder();
				sb.append(twoHyphens + BOUNDARY + lineEnd);
				sb.append("Content-Disposition: form-data; name=\"files\";filename=\""
						+ list.get(i).getId() + ".jpg\"" + lineEnd);
				sb.append(lineEnd);
				outputStream.write(sb.toString().getBytes());

				outputStream.write(list.get(i).getImages());
				outputStream.write("\r\n".getBytes());
			}
			outputStream.writeBytes(lineEnd);
			outputStream.writeBytes(twoHyphens + BOUNDARY + twoHyphens
					+ lineEnd);

			int responseCode = connection.getResponseCode();
			if (responseCode == HttpURLConnection.HTTP_OK) {
				/* 取得Response内容 */
				InputStream is = connection.getInputStream();
				int ch;
				StringBuffer b = new StringBuffer();
				while ((ch = is.read()) != -1) {
					b.append((char) ch);
				}
				/* 将Response显示于Dialog */
				Util.print("上传成功" + b.toString().trim());

				return b.toString().trim();

			} else {

				Util.print("请求服务器失败:  " + responseCode);
			}

			/* 关闭DataOutputStream */
			outputStream.flush();
			outputStream.close();

		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return null;
	}

}
